﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using System.Data.SqlClient;

namespace Runda.EShop.Dal
{
    /// <summary>
    /// DAL layer class dbo.Biz_OrderDetail_Patch.
    /// </summary>
    public partial class BizOrderDetailPatchDal
    {
        public void BulkInsert(List<BizOrderDetailPatchEntity> list)
        {
            SqlBulkCopy bulkCopy;

            if (base.Tran != null)
            {
                SqlConnection cnx = Tran.Connection as SqlConnection;
                SqlTransaction transaction = Tran as SqlTransaction;
                bulkCopy = new SqlBulkCopy(cnx, SqlBulkCopyOptions.CheckConstraints, transaction);
            }
            else
                bulkCopy = new SqlBulkCopy(CurrentDatabase.ConnectionString, SqlBulkCopyOptions.CheckConstraints);

            bulkCopy.BulkCopyTimeout = 360;
            bulkCopy.DestinationTableName = "Biz_OrderDetail_Patch";

            DataTable dataTable = new DataTable();
            DataColumn col0 = dataTable.Columns.Add("ID", typeof(System.Guid));
            col0.AllowDBNull = false;
            DataColumn col1 = dataTable.Columns.Add("OrderID", typeof(System.Guid));
            col1.AllowDBNull = true; 
            DataColumn col3 = dataTable.Columns.Add("BatchCode", typeof(System.String));
            col3.AllowDBNull = true;
            DataColumn col4 = dataTable.Columns.Add("DueDate", typeof(System.DateTime));
            col4.AllowDBNull = true;
            DataColumn col5 = dataTable.Columns.Add("AssignQty", typeof(System.Decimal));
            col5.AllowDBNull = true;
            DataColumn col6 = dataTable.Columns.Add("ActualQty", typeof(System.Decimal));
            col6.AllowDBNull = true;
            DataColumn col7 = dataTable.Columns.Add("ConfirmStatus", typeof(System.String));
            col7.AllowDBNull = true;
            DataColumn col8 = dataTable.Columns.Add("Deleted", typeof(System.Boolean));
            col8.AllowDBNull = true;
            DataColumn col9 = dataTable.Columns.Add("DetailID", typeof(System.Guid));
            col9.AllowDBNull = true;
            DataColumn col10 = dataTable.Columns.Add("ProductID", typeof(System.Guid));
            col10.AllowDBNull = true;
            DataColumn col11 = dataTable.Columns.Add("ProductCode", typeof(System.String));
            col11.AllowDBNull = true; 

            bulkCopy.ColumnMappings.Add("ID", "ID");
            bulkCopy.ColumnMappings.Add("OrderID", "OrderID"); 
            bulkCopy.ColumnMappings.Add("BatchCode", "BatchCode");
            bulkCopy.ColumnMappings.Add("DueDate", "DueDate");
            bulkCopy.ColumnMappings.Add("AssignQty", "AssignQty");
            bulkCopy.ColumnMappings.Add("ActualQty", "ActualQty");
            bulkCopy.ColumnMappings.Add("ConfirmStatus", "ConfirmStatus");
            bulkCopy.ColumnMappings.Add("Deleted", "Deleted");
            bulkCopy.ColumnMappings.Add("DetailID", "DetailID");
            bulkCopy.ColumnMappings.Add("ProductID", "ProductID");
            bulkCopy.ColumnMappings.Add("ProductCode", "ProductCode"); 

            foreach (BizOrderDetailPatchEntity entity in list)
            {
                DataRow row = dataTable.NewRow();

                row["ID"] = entity.ID;
                row["OrderID"] = entity.OrderID != Guid.Empty ? (object)entity.OrderID : System.DBNull.Value; 
                row["DueDate"] = entity.DueDate != DateTime.MinValue ? (object)entity.DueDate : System.DBNull.Value;
                row["AssignQty"] = entity.AssignQty != Decimal.MinValue ? (object)entity.AssignQty : System.DBNull.Value;
                row["ActualQty"] = entity.ActualQty != Decimal.MinValue ? (object)entity.ActualQty : System.DBNull.Value;
                row["ConfirmStatus"] = entity.ConfirmStatus;
                row["BatchCode"] = entity.BatchCode;
                row["Deleted"] = (object)entity.Deleted;
                row["DetailID"] = entity.DetailID != Guid.Empty ? (object)entity.DetailID : System.DBNull.Value;
                row["ProductID"] = entity.ProductID != Guid.Empty ? (object)entity.ProductID : System.DBNull.Value;
                row["ProductCode"] = entity.ProductCode;

                dataTable.Rows.Add(row);
            }

            bulkCopy.WriteToServer(dataTable);
        }

        public DataTable GetOrderDetailPatch(Guid OrderID)
        {
            string sql = @"select A.ID,A.OrderID,A.BatchCode,A.DueDate,A.AssignQty,A.ActualQty,A.ConfirmStatus
,C.ProductCode,C.ProductName
from Biz_OrderDetail_Patch A 
JOIN Bas_Product C ON A.ProductID = C.ProductID WHERE A.OrderID=@OrderID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@OrderID", DbType.Guid, OrderID);

            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public void UpdateOrderDetailPatchStatus(Guid ID, string ConfirmStatus,decimal ActualQty)
        {
            string sql = "UPDATE Biz_OrderDetail_Patch SET ActualQty=@ActualQty,ConfirmStatus=@ConfirmStatus WHERE ID=@ID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@ID", DbType.Guid, ID);
            CurrentDatabase.AddInParameter(dbcommand, "@ActualQty", DbType.Decimal, ActualQty);
            CurrentDatabase.AddInParameter(dbcommand, "@ConfirmStatus", DbType.String, ConfirmStatus);

            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }
    }
}
